<?php
class Verify extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        header("Access-Control-Allow-Origin: *");//允许跨域
        $this->load->helper("url");
    }

    //验证令牌
    public function verifyPsw()
    {
        if (!isset($_POST['pass']) || empty($_POST['pass']))
        {
            displayJson(400,'错误！！！');
        }

        if ($_POST['pass'] == _PASS_)
        {
            displayJson(200,'通过');
        } else {
            displayJson(400,'错误！！！');
        }
    }

    //审核页面
    public function verifyHtml()
    {
        if (!isset($_GET['pass']) || $_GET['pass'] != _PASS_)
        {
            echo '非法进入！';die;
        }

        $this->load->database();

        $this->db->select('id,image,submit_time');
        $this->db->from('verify_log');
        $this->db->where('status',1);
        $query  = $this->db->get();
        $list   = $query->result_array();

        $this->db->close();

        //去掉自己
        foreach ($list as $k => &$v)
        {
            $v['image'] = base_url().$v['image'];
        }

        $data['pass'] = _PASS_;
        $data['list'] = $list;

        $this->load->view("verifyHtml",$data);
    }

    //审核结果
    public function changeStatus()
    {
        if (
            !isset($_POST['id']) || empty($_POST['id']) ||
            !isset($_POST['status']) || empty($_POST['status'])
        ) {
            displayJson(400,'参数设置错误');
        }

        $this->load->database();

        $this->db->select('image');
        $this->db->where('id',$_POST['id']);
        $query = $this->db->get('verify_log');
        $check = $query->row();

        if (empty($check))
        {
            $this->db->close();
            displayJson(400,'数据异常');
        }

        $this->db->trans_start();

        $this->db->set('status', $_POST['status']);
        $this->db->set('verify_time', date('Y-m-d H:i:s'));
        $this->db->where('id', $_POST['id']);
        $this->db->update('verify_log');

        if ($_POST['status'] == 2)
        {
            //添加新头像
            $data = [
                'url' => $check->image,
            ];
            $this->db->insert('avatar', $data);
        }

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            $this->db->close();
            displayJson(400,'服务器异常');
        }

        $this->db->close();

        displayJson(200,'操作成功');
    }

}